package com.example.demomk.Fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.example.demomk.Adapter.DetailsAdapter;
import com.example.demomk.R;
import com.example.demomk.ViewModel.MeasurementViewModel;
import com.example.demomk.databinding.FragmentChildDetailsBinding;


public class ChildDetails extends Fragment {
    private DetailsAdapter adapter;

    private RecyclerView recyclerView;
    private MeasurementViewModel viewmodel;
    private FragmentChildDetailsBinding binding;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "aram2";

    // TODO: Rename and change types of parameters
    private int mParam1;
    private int page ;

    public ChildDetails() {
        // Required empty public constructor
    }


    public static ChildDetails newInstance(int param1) {
        ChildDetails fragment = new ChildDetails();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getInt(ARG_PARAM1);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentChildDetailsBinding.inflate(inflater,container,false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        FragmentActivity fragmentActivity = requireActivity();

        if (mParam1 == 0 ){
            mParam1 = 1;
        }

        initView();
        adapter(fragmentActivity);
    }

    private void initView() {
        viewmodel = new ViewModelProvider(this).get(MeasurementViewModel.class);
        recyclerView = binding.detailRecycler;
        adapter = new DetailsAdapter();

        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));


        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.custom_divider));
        recyclerView.addItemDecoration(dividerItemDecoration);
    }

    private void adapter(FragmentActivity fragmentActivity) {

        viewmodel.getAvgMeasurementWithDetails(mParam1).observe(fragmentActivity, avgMeasurementWithMeasurements -> {

            if (avgMeasurementWithMeasurements != null) {
                adapter.setMeasurements(avgMeasurementWithMeasurements.measurements);
            }
        });

        viewmodel.getMeasurementCount().observe(fragmentActivity, count -> {
            if (count != null) {
                String countString = String.valueOf(count);
                binding.count.setText("样本数: "+ countString);
            }
        });

    }

}